package per.destiny.seckill.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import per.destiny.seckill.service.UserService;
import per.destiny.seckill.vo.LoginVo;
import per.destiny.seckill.vo.RespBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/toLogin", method = RequestMethod.GET)
    public String toLogin() {
        return "login";
    }

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public RespBean doLogin(@Valid @RequestBody LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        log.info("loginVo is {}", loginVo);
//        if (StringUtils.isBlank(loginVo.getMobile()) || StringUtils.isBlank(loginVo.getPassword())) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if (!ValidatorUtils.isMobile(loginVo.getMobile())) {
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        RespBean respBean = userService.doLogin(loginVo, request, response);
        return respBean;
    }


}
